// toolbar工具栏的工具选项（默认展示全部）
const toolOptions = [
  ['bold', 'italic', 'underline', 'strike'],
  ['blockquote', 'code-block'],
  [{'header': 1}, {'header': 2}],
  [{'list': 'ordered'}, {'list': 'bullet'}],
  [{'script': 'sub'}, {'script': 'super'}],
  [{'indent': '-1'}, {'indent': '+1'}],
  [{'direction': 'rtl'}],
  [{'size': ['small', false, 'large', 'huge']}],
  [{'header': [1, 2, 3, 4, 5, 6, false]}],
  [{'color': []}, {'background': []}],
  [{'font': []}],
  [{'align': []}],
  [{'clean': '源码编辑'}],
  ['link', 'image', 'video'],
  ['sourceEditor'], // 新添加的工具
  ['emoji'] // 表情
]
const handlers = {
  shadeBox: null,
  sourceEditor: function () { // 添加工具方法
    const reg = /\<br\>/g
    const container = this.container
    const firstChild = container.nextElementSibling.firstChild
    if (!this.shadeBox) {
      let shadeBox = this.shadeBox = document.createElement('div')
      shadeBox.style.cssText = 'position:absolute; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.5); cursor:pointer'
      container.style.position = 'relative'
      shadeBox.addEventListener('click', function () {
        this.style.display = 'none'
        firstChild.innerHTML = firstChild.innerText.trim()
      }, false)
      container.appendChild(shadeBox)
      let innerHTML = firstChild.innerHTML
      innerHTML = innerHTML.replace(reg, '')
      firstChild.innerText = innerHTML
    } else {
      let innerHTML = firstChild.innerHTML
      innerHTML = innerHTML.replace(reg, '')
      firstChild.innerText = innerHTML
      this.shadeBox.style.display = 'block'
    }
  },
  emoji: function () {
    alert(123)
  }
}

export default {
  placeholder: '',
  theme: 'snow', // 主题 snow bubble
  modules: {
    toolbar: {
      container: toolOptions, // 工具栏选项
      handlers: handlers // 事件重写
    }
  },
  initButton: function () { // 在使用的页面中初始化按钮样式
    const sourceEditorButton = document.querySelector('.ql-sourceEditor')
    // sourceEditorButton.style.cssText = 'width:80px; border:1px solid #ccc; border-radius:5px;'
    sourceEditorButton.classList.add('el-icon-edit-outline')
    // sourceEditorButton.innerText = '表情'
    sourceEditorButton.title = '源码编辑'

    const emojiButton = document.querySelector('.ql-emoji')
    emojiButton.style.cssText = 'width:50px; border:1px solid #ccc; border-radius:5px;'
    emojiButton.innerText = '表情'
  }
}
